define(["angular",
    "framework/utils/JSONPrettyFormat",
    'framework/dataGrid/DataGridModule',
    'framework/pagination/GillionPaginationModule',
    'framework/msg/GillionMsgModule'
], function (angular, JSONPrettyFormat) {
    /**
     *  Module
     *
     * Description
     */
    var RuleModule = angular.module('RuleModule', ['DataGridModule',
        'GillionPaginationModule',
        'GillionMsgModule'
    ]);

    RuleModule.controller('RuleController', function ($scope, Resource, $dataSourceManager, GillionMsg, $location) {
        var ruleResource = Resource("/grule-manager/rule/:id", {
            id: "@id"
        });
        $scope.ruleAssemblyId = $location.search().ruleAssemblyId;
        $scope.search = {
            ruleAssemblyId:  $scope.ruleAssemblyId
        };

        Resource("/grule-manager/ruleAssembly/:id", {id: '@id'}).get({
            id: $scope.ruleAssemblyId
        }, function (ruleAssembly) {
            $scope.ruleAssembly = ruleAssembly;
        });

        $scope.searchRule = function () {
            $scope.search = $scope.search || {};
            var params = angular.extend($scope.search);
            $dataSourceManager.dataSources.ruleSource.doRequestData(1, params);
        }
        $scope.showPageView = function () {
            $scope.showPage = true;
            $scope.showAdd = false;
            $scope.showEdit = false;
        };
        $scope.showEditView = function () {
            $scope.showPage = false;
            $scope.showAdd = false;
            $scope.showEdit = true;
        };

        $scope.ruleRendered = function (grid) {
            grid.actions.edit = function (row) {
                ruleResource.get({
                    id: row.id
                }, function (result) {
                    delete result.$promise;
                    delete result.$resolved;
                    $scope.currId = result.id;
                    delete result.id;
                    if (result.expression) {
                        result.expression = JSON.parse(result.expression);
                    }
                    $scope.editRule = JSONPrettyFormat(result);
                    $scope.showEditView();
                });
            };
        };

        $scope.showAddView = function () {
            $scope.showPage = false;
            $scope.showAdd = true;
            $scope.showEdit = false;

            var templateRule = {
                "name": "【必填】",
                "expression": "【json格式】",
                "priority": "【整型】",
                "ruleAssemblyId": $scope.ruleAssemblyId,
                "remark": ""
            };

            $scope.addRule = JSONPrettyFormat(templateRule);
        };

        $scope.createRule = function () {
            var rule = JSON.parse($scope.addRule);
            if (rule.expression) {
                rule.expression = JSON.stringify(rule.expression);
            }
            ruleResource.create(rule, function (response) {
                if (response.success) {
                    GillionMsg.alert("提示信息", "保存成功");
                    $scope.search = $scope.search || {};
                    var params = angular.extend($scope.search);
                    $dataSourceManager.dataSources.ruleSource.doRequestData(1, params);
                    $scope.showPageView();
                }else if (response.success !== false) {
                    GillionMsg.alert("提示消息", "保存失败，请核对保存信息！");
                }
            });        };

        $scope.updateRule = function () {
            var rule = JSON.parse($scope.editRule);
            if (rule.expression) {
                rule.expression = JSON.stringify(rule.expression);
            }
            rule.id = $scope.currId;
            ruleResource.update(rule, function (response) {
                if (response.success) {
                    GillionMsg.alert("提示信息", "保存成功");
                    $scope.search = $scope.search || {};
                    var params = angular.extend($scope.search);
                    $dataSourceManager.dataSources.ruleSource.doRequestData(1, params);
                    $scope.showPageView();
                }else if (response.success !== false) {
                    GillionMsg.alert("提示消息", "更新失败，请核对更新信息！");
                }
            });
        };

        $scope.onRemoveRecord = function (event, row) {
            GillionMsg.confirm('提示信息', '是否确定删除？', function(r){
                if (r) {
                    ruleResource.remove({
                        id: row.id
                    }, function (response) {
                        if (response.success) {
                            GillionMsg.alert("提示消息", "删除成功！");
                            $scope.searchRule();
                        }
                    });
                }
            });
            return false;
        };

        $scope.showPageView();
    });
});